// Copyright (c) Appeon. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.

using System.Collections.Generic;
using System.Data;
using JetBrains.Annotations;
using Npgsql;

namespace SnapObjects.Data.PostgreSql
{
    internal class PostgreSqlAdoDbProvider : AdoDbProvider
    {
        public PostgreSqlAdoDbProvider(DataContext dataContext) :
            base(dataContext)
        {
        }

        protected override IAdoDbResultSet<TModel> CreateResultSet<TModel>(
            IDbCommand command,
            IDataReader reader,
            IEnumerable<IAdoDbParameter> parameters)
        {
            var visitor = new PostgreSqlAdoDbDataVisitor(
                (NpgsqlCommand)command, (NpgsqlDataReader)reader, parameters, _dataContext.ContextOptions);

            var mapper = new PostgreSqlTypeMapper(_dataContext.ContextOptions);

            return new PostgreSqlAdoDbResultSet<TModel>(visitor, mapper);
        }

        protected override IAdoDbDataVisitor CreateDataVisitor(IDbCommand command,
            IDataReader reader,
            IEnumerable<IAdoDbParameter> parameters)
        {
            return new PostgreSqlAdoDbDataVisitor(
                (NpgsqlCommand)command, (NpgsqlDataReader)reader, parameters, _dataContext.ContextOptions);
        }

        protected override IAdoDbCommand CreateAdoDbCommand(
            [NotNull] string sqlText,
            IEnumerable<IAdoDbParameter> parameters,
            CommandType commandType = CommandType.Text,
            bool addExceptionToTransaction = true)
        {
            return new PostgreSqlAdoDbCommand(sqlText, parameters, addExceptionToTransaction, commandType);
        }
    }
}
